package com.dingwen.treasure.base.valid;

import cn.hutool.core.util.StrUtil;
import com.dingwen.treasure.base.annotation.BanValues;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * 　BanValuesValidator
 * 　@author dingwen
 * 　@date 2022/6/27
 */
public class BanValuesValidator implements ConstraintValidator<BanValues, Object> {

    /**
     *  禁止含有的字符
     */
    private String[] banValues;

    @Override
    public void initialize(BanValues banValues) {
        this.banValues = banValues.banValues();
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        return !Arrays.stream(banValues).parallel().anyMatch(ban -> StrUtil.toString(o).contains(ban));
    }
}
